<?php namespace Admin\Controller;
class GoodsController extends CommonController{
	protected $db;
	public function __construct(){
		$this->db = new \Common\Model\Goods;
		parent::__construct();
	}
	public function index(){
		//总计几条
		$count = $this->db->count();
		$page = Page::row(8)->make($count);
		$goods = $this->db->limit(page::limit())->get();
		View::with("goods",$goods);
		View::with("page",$page);
		View::make();
		
	}
	/**
	 * 异步调取规格和属性
	 */
	public function ajaxGetAttr(){
		if(IS_AJAX){
			$type_id = $_POST['type_id'];
			
			//type_ID,是类型属性表当中的类型id
			$typeAttrData = Db::table("360mall_type_attr")
			->where("ta_type_id",$type_id)->get();
			foreach ($typeAttrData as $k => $v) {
				$typeAttrData[$k]['ta_value']=explode("|", $v['ta_value']);
			}
//			View::ajax($typeAttrData);
			echo json_encode($typeAttrData);
		}
	}
	
	
	
//异步上传
public function ajaxUpload(){
	$files = Upload::path("Upload/goods/".date("Ymd"))->make();
	print_r($files[0]['path']);
	}

//异步删除上传图片
public function delImg(){
	if(IS_AJAX){
		$path =  $_POST['path'];
		if(unlink($path)){
			View::ajax(1);
		}
		View::ajax(0);
	}

}
//异步上传图册
public function ajaxUploadImg(){
	$path = "Upload/gImg/".date("Ymd");
	$files = Upload::path($path)->make();
echo $files[0]['path'];
//echo json_encode($img);
}
	
public function add(){
		//获得所有分类
		$cateData = Db::table("360mall_category")->get();	
		$cateData = Data::tree($cateData,"cate_name",$fieldPri = "cate_id",$fieldPid ="cate_pid");
		View::with("cateData",$cateData);
		//获得所有品牌
		$brandData = Db::table("360mall_brand")->get();	
		View::with("brandData",$brandData);
		if(IS_POST){
//		p($_POST);die;
			if(!$this->db->store()){
				View::error($this->error->getError());
				
			}else{
				
			View::success("添加成功",U('Goods/index'));	
			}
		}
		View::make();
	}
//编辑
		public function edit(){
			$gid = 	Q("get.gid",0,'intval');
			//获得旧数据
			$olddata = $this->db->where("g_id",$gid)->first();
			$goodAttrData = Db::table("360mall_goods_attr")->where("ga_gid",$gid)->join("360mall_type_attr","ga_taid","=","ta_id")->where("class",0)->get();
			
			$goodSpecData = Db::table("360mall_goods_attr")->where("ga_gid",$gid)->join("360mall_type_attr","ga_taid","=","ta_id")->where("class",1)->get();
			foreach ($goodAttrData as $k => $v) {
				$goodAttrData[$k]['ta_value']=explode("|", $v['ta_value']);
			}
			foreach ($goodSpecData as $k => $v) {
				$goodSpecData[$k]['ta_value']=explode("|", $v['ta_value']);
			}
			//获得图片详情数据
			$detaildata = Db::table("360mall_gdetail")->where("gd_gid",$gid)->get();
			foreach ($detaildata as $key => $value) {
				$detaildata[$key]['gd_small'] = explode(",", $value['gd_small']);
				$detaildata[$key]['gd_medium'] = explode(",", $value['gd_medium']);
				$detaildata[$key]['gd_big'] = explode(",", $value['gd_small']);
			}
			//获得数据属性
			View::with("goodSpecData",$goodSpecData);
			View::with("goodAttrData",$goodAttrData);
			View::with("detaildata",$detaildata);
			View::with("olddata",$olddata);
		//获得所有分类
		$cateData = Db::table("360mall_category")->get();	
		$cateData = Data::tree($cateData,"cate_name",$fieldPri = "cate_id",$fieldPid ="cate_pid");
		View::with("cateData",$cateData);
		//获得所有品牌
		$brandData = Db::table("360mall_brand")->get();	
		View::with("brandData",$brandData);
		if(IS_POST){
			if(!$this->db->edit($gid)){
				View::error($this->error->getError());
				
			}else{
				
			View::success("编辑成功",U('Goods/index'));	
			}
		}
			View::make();
		}
		public function del(){
			$gid = 	Q("get.gid",0,'intval');
			$this->db->del($gid);
			$this->success("删除成功",U('Goods/index'));
		}
}